#include <iostream>
#include <pthread.h>
#include <cstdio>
#include <vector>
#include <string>
#include <unistd.h>

const int num=10;

void* routine(void* args)
{
    sleep(1);
    std::string name=static_cast<const char*>(args);
    delete[] (char*)args;

    int cnt=5;
    while(cnt--)
    {
        std::cout<<"new线程name: "<<name<<std::endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    std::vector<pthread_t> tids;

    for(int i=0;i<num;i++)
    {
        pthread_t tid;
        char* id=new char[64];
        snprintf(id,64,"thread-%d",i);

        int n = pthread_create(&tid,nullptr,routine,(void*)id);
        if(n==0)
        {
            tids.push_back(tid);
        }
        else
        {
            continue;
        }
    }

    for(int i=0;i<num;i++)
    {
        int n=pthread_join(tids[i],nullptr);
        
        if(n==0)
        {
            std::cout<<"thread "<<i<<" join success"<<std::endl;
        }
        else
        {
            std::cout<<"thread "<<i<<" join failed"<<std::endl;
        }
    }

    return 0;
}